import {
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
    GraphQLNonNull,
    GraphQLBoolean,
    GraphQLInt

} from 'graphql';

import Kind from '../../db/models/kind'

export let kindType = new GraphQLObjectType({
    name: 'kind',
    fields: {
        id: {
            type: GraphQLInt
        },
        kind_name: {
            type: GraphQLString
        },
        kind_code: {
            type: GraphQLString
        }
    }
})

export const kindList = {
    type: new GraphQLList(kindType),
    args: {},
    resolve(root, params, options) {
        return Kind.findAll()
    }
}

export const kindOne = {
    type: kindType,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Kind.findOne({
            where: {
                id: params.id
            }
        })
    }
}
export const kindUpdate = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        },
        kind_name: {
            type: GraphQLString
        },
        kind_code: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Kind.update({
            kind_name: params.kind_name,
            kind_code: params.kind_code


        }, {
            where: {
                id: params.id
            }
        })
    }
}

export const kindAdd = {
    type: kindType,
    args: {
        kind_name: {
            type: GraphQLString
        },
        kind_code: {
            type: GraphQLString
        },
        parent_id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Kind.create({
            kind_name: params.kind_name,
            kind_code: params.kind_code
        })
    }
}

export const kindDel = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Kind.destroy({
            where: {
                id: params.id
            }
        })
    }
}
